package vistas;

import interfaces.Notificador;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import api.ControlDispositivos;
import api.Dispositivo;

public class VistaAgregarPiso extends JFrame implements MouseListener, ActionListener {

	
	@SuppressWarnings("rawtypes")

	private VistaEdificio vistaEdificio;
	
	private Pattern patron = Pattern.compile("[a-z|A-Z|0-9]*");
	private int clickX;
	private int clickY;
	
	private Container container;
	private GridBagConstraints csVista = new GridBagConstraints();	

	// ID del plano
	private JTextField txtNombre;
	private JLabel lblNombre;
	private String piso;
	
	// Imagen
	private JTextField txtNombreImagen;
	private JLabel lblImagen;
	private JButton buttExaminar; 
	
	//Botones generales
	private JButton buttAceptar;
	private JButton buttCancelar;
	
	//FileChooser
	private JFileChooser fileChooser;
	
	private int sizeW = 600;
	private int sizeH = 150;
	
	public VistaAgregarPiso(VistaEdificio ve){
		super("VistaAgregarPiso");
		this.vistaEdificio = ve;
		this.setContainer();	
		this.setVisible(false);
	}

	public void setContainer(){		
		container = new Container();
		this.container.setLayout(new GridBagLayout());		
		
		this.lblNombre = new JLabel("Nombre");
		this.txtNombre = new JTextField();		
		
		this.lblImagen = new JLabel("Archivo de Imagen");
		this.txtNombreImagen = new JTextField();

		this.setCsVista(0,0,1,1,1.0,0.0,GridBagConstraints.NONE,GridBagConstraints.CENTER);		
		this.container.add(this.lblNombre,csVista);
		this.setCsVista(0,1,1,1,0.5,0.0,GridBagConstraints.BOTH,GridBagConstraints.CENTER);		
		this.container.add(this.txtNombre,csVista);
						
		this.setCsVista(1,0,1,1,1.0,0.0,GridBagConstraints.NONE,GridBagConstraints.CENTER);		
		this.container.add(this.lblImagen,csVista);
		this.setCsVista(1,1,1,1,0.0,0.0,GridBagConstraints.BOTH,GridBagConstraints.CENTER);		
		this.container.add(this.txtNombreImagen,csVista);
		this.setCsVista(2,1,1,1,0.25,0.0,GridBagConstraints.BOTH,GridBagConstraints.CENTER);		
		this.container.add(this.getButtExaminar(),csVista);
		
		this.setCsVista(0,3,1,1,0.0,0.0,GridBagConstraints.SOUTH,GridBagConstraints.EAST);		
		this.container.add(this.getButtAceptar(),csVista);
		this.setCsVista(1,3,1,1,0.0,0.0,GridBagConstraints.SOUTH,GridBagConstraints.WEST);		
		this.container.add(this.getButtCancelar(),csVista);
		
		this.setContentPane(container);
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);					
		this.pack();
		this.setSize(this.sizeW, this.sizeH);		
	}
	
	public void setNombreImagen(String texto){
		this.txtNombreImagen.setText(texto);
		this.repaint();
	}
	
	public void setClickXY(int x, int y){
		this.clickX = x;
		this.clickY = y;
	}

	private void setCsVista(int x, int y, int cantCeldas, int cantLineas, double largoCelda, double altoCelda, int fill, int anchor){
		this.csVista.gridx = x;
		this.csVista.gridy = y;
		this.csVista.gridwidth = cantCeldas;
		this.csVista.gridheight = cantLineas;
		this.csVista.weightx = largoCelda;
		this.csVista.weighty = altoCelda;
		this.csVista.fill = fill;
		this.csVista.anchor = anchor;
	}
	
	private JButton getButtAceptar(){
		if (this.buttAceptar == null) {
			this.buttAceptar = new JButton("Aceptar");
			this.buttAceptar.addMouseListener(this);
		}		
		return this.buttAceptar;
	}
	
	private JButton getButtCancelar(){
		if (this.buttCancelar == null) {
			this.buttCancelar = new JButton("Cancelar");
			this.buttCancelar.addMouseListener(this);
		}		
		return this.buttCancelar;
	}
	
	private JButton getButtExaminar(){
		if (this.buttExaminar == null){
			this.buttExaminar = new JButton("Examinar");
			this.buttExaminar.addMouseListener(this);
		}
		return this.buttExaminar;
	}

	public void mouseClicked(MouseEvent arg0) {		
		String nombre = arg0.getComponent().getAccessibleContext().getAccessibleName();		
		if (nombre.compareTo(this.buttCancelar.getText()) ==  0) {
			this.setVisible(false);
		}
		else if (nombre.compareTo(this.buttAceptar.getText()) ==  0) {			
			if ((Pattern.matches(".*[a-z|A-Z|0-9]*",this.txtNombre.getText()))){	
					PlanoEdificio planoAgregado = new PlanoEdificio(this.txtNombre.getText(),this.txtNombreImagen.getText());
					this.vistaEdificio.agregarPlano(planoAgregado);
					this.limpiar();
					this.repaint();
					this.setVisible(false);	
			}	
					
		}
		else if (nombre.compareTo(this.buttExaminar.getText()) ==  0) {
			this.fileChooser = new JFileChooser();
			int resultado = this.fileChooser.showOpenDialog(this);
			if (resultado == JFileChooser.APPROVE_OPTION) {
				this.txtNombreImagen.setText(this.fileChooser.getSelectedFile().getAbsolutePath());
			}
		}		
	}
	
	public void limpiar(){
		this.txtNombre.setText("");
		this.txtNombreImagen.setText("");
	}
	
	public void setVistaEdificio(VistaEdificio ve) {
		this.vistaEdificio = ve;
	}
	
	public void actionPerformed(ActionEvent arg0) {}
	public void mouseEntered(MouseEvent arg0) {}
	public void mouseExited(MouseEvent arg0) {}
	public void mousePressed(MouseEvent arg0) {}
	public void mouseReleased(MouseEvent arg0) {}
	

}
